//
//  uiNodeLine.hpp
//  smu_test1
//
//  Created by jiayi on 2016/10/2.
//
//

#ifndef uiNodeLine_hpp
#define uiNodeLine_hpp

#include "uiCommon.hpp"
#include "uiNodeLine.h"
bool uiNodeLine::init()
{
    return DrawNode::init();
}

void uiNodeLine::updateLine()
{
    if( !_root || _root->isFake() ){
        return;
    }
    clear();
    
    if( _root->getNodeData() && !_root->getNodeData()->isAvailable() ){
        return;
    }
    
    int z = _root->getLocalZOrder();
    for( int i = 0 ; i < _root->_relyFrom.size(); ++ i ){
        if( _root->_relyFrom[i]->isFake() ){
            continue;
        }
        Vec2 fromPos = convertToNodeSpace( _root->_relyFrom[i]->getWorldPosition() );
        Vec2 toPos = convertToNodeSpace( _root->getWorldPosition() );
        /// draw line between two nodes
        drawLine( fromPos, toPos, Color4F::GRAY );
        /// draw line end
        
        /// draw arrow
        Vec2 direction = (toPos - fromPos).getNormalized();
        Vec2 middle = (toPos + fromPos) / 2;
        Vec2 offsetMiddle = middle + 2 * direction;
        
        Vec2 target1 = offsetMiddle.rotateByAngle( middle, 10 );
        Vec2 target2 = offsetMiddle.rotateByAngle( middle, -10 );
        
        drawLine( target1, middle, Color4F::GRAY );
        drawLine( target2, middle, Color4F::GRAY );
        /// draw arrow end
        
        z = min( _root->_relyFrom[i]->getLocalZOrder() - 1, z );
    }
    
    // avoid overlap
    _root->setLocalZOrder( z );
}

#endif /* uiNodeLine_hpp */
